﻿using MMS.Model.FormatModel;
using MMS.UI.Extension;
using MMS.UI.Helper;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.WebPages;

namespace MMS.UI.Areas.Admin.Controllers
{
    public class SalesAnalysisController : BaseController
    {
        #region 1.0 加载 商品库存排名 图表
       
        #endregion

        #region 2.0 加载 商品销售排名 图表
        public ActionResult GoodsSale()
        {
            var list = OpeCur.GetAsync<List<class_SaleItem>>("http://localhost:16824/api/SalesAnalysis/GetAllGoodsSale");
            ViewBag.AllSaleItem = OpeCur.ToJson(list);
            return View();
        }
        [HttpPost]
        public ActionResult GoodsSale(FormCollection form)
        {
            int pageIndex = Request.Form["page"].AsInt();
            int pageSize = Request.Form["rows"].AsInt();
            string sort = Request.Form["sort"] == null ? "Total" : Request.Form["sort"];
            string order = Request.Form["order"] == null ? "desc" : Request.Form["order"];
            var pageData = OpeCur.GetAsync<PageData<class_SaleItem>>("http://localhost:16824/api/SalesAnalysis/GetGoodsSaleWherePaged/" + pageIndex + "/" + pageSize + "/" + sort + "/" + order);
            //2.转成json格式字符串
            var strJson = OpeCur.ToJson(pageData);
            return Content(strJson);
        }
        #endregion

        #region 3.0 加载 销售趋势分析 图表
        public ActionResult Trend()
        {
            return View();
        }
        #endregion

        #region 4.0 加载 品类销售分析 图表
        public ActionResult Category()
        {
            var list = OpeCur.GetAsync<List<class_category>>("http://localhost:16824/api/SalesAnalysis/GetAllCategory");
            ViewBag.Category = OpeCur.ToJson(list);
            return View();
        }
        #endregion

        #region 5.0 加载 会员推广与销售统计 图表
        public ActionResult PromotionAndSales()
        {
            var list = OpeCur.GetAsync<List<class_PromotionAndSales>>("http://localhost:16824/api/SalesAnalysis/GetPromotionAndSales/2000-01-01/2030-01-01");
            ViewBag.PromotionAndSales = OpeCur.ToJson(list);
            return View();
        }
        [HttpPost]
        public ActionResult PromotionAndSales(FormCollection form)
        {
            string startdate = Request.Form["startdate"];
            string enddate = Request.Form["enddate"];
            startdate = DateTime.ParseExact(startdate, "yyyy-MM-dd HH:mm:ss", null).ToString("yyyyMMddHHmmss");
            enddate = DateTime.ParseExact(enddate, "yyyy-MM-dd HH:mm:ss", null).ToString("yyyyMMddHHmmss");

            var Data = OpeCur.GetAsync<List<class_PromotionAndSales>>("http://localhost:16824/api/SalesAnalysis/GetPromotionAndSales/" + startdate + "/" + enddate);

            var strJson = OpeCur.ToJson(Data);
            return Content(strJson);
        }
        #endregion

        #region 6.0 加载 会员消费分阶人数统计表 图表
        public ActionResult RankOrder()
        {
            var list = OpeCur.GetAsync<List<class_RankOrder>>("http://localhost:16824/api/SalesAnalysis/GetRankOrder/2017-01-01");
            ViewBag.RankOrder = OpeCur.ToJson(list);
            return View();
        }
        [HttpPost]
        public ActionResult RankOrder(FormCollection form)
        {
            string date = Request.Form["date"];
            var Data = OpeCur.GetAsync<List<class_RankOrder>>("http://localhost:16824/api/SalesAnalysis/GetRankOrder/" + date);

            var strJson = OpeCur.ToJson(Data);
            return Content(strJson);
        }
        #endregion

        #region 7.0 加载 会员月度购物频次统计 图表
        public ActionResult ShoppingFrequency()
        {
            var list = OpeCur.GetAsync<List<class_ShoppingFrequency>>("http://localhost:16824/api/SalesAnalysis/GetShoppingFrequency/2017/广东茂名");
            ViewBag.ShoppingFrequency = OpeCur.ToJson(list);
            return View();
        }
        [HttpPost]
        public ActionResult ShoppingFrequency(FormCollection form)
        {
            string date = Request.Form["date"];
            string address = Request.Form["address"];

            var Data = OpeCur.GetAsync<List<class_ShoppingFrequency>>("http://localhost:16824/api/SalesAnalysis/GetShoppingFrequency/" + date + "/" + address);

            var strJson = OpeCur.ToJson(Data);
            return Content(strJson);
        }
        #endregion

    }
}
